<template>
  <div class="app-container">
    <div style="width:100%; text-align: right;">
      <el-button size="small" type="primary" icon="el-icon-download" @click="exportData">导出</el-button>
    </div>

    <div style="width: 100%; margin-top: 8px;">
      <el-form :model="searchForm" label-width="80px">
        <div style="display: flex;">
          <el-form-item label="关键字">
            <el-input v-model="searchForm.searchValue" size="small" placeholder="车牌号 | 司机 | 接待人" style="width: 160px;"></el-input>
          </el-form-item>
          <!--
          <el-form-item label="接待部门">
            <el-select v-model="searchForm.deptId" placeholder="请选择">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          -->
          <el-form-item label="审批状态" style="margin-left: 10px;width: 170px;">
            <el-select v-model="searchForm.status" placeholder="请选择">
              <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间" style="margin-left: 10px;">
            <el-date-picker
                v-model="searchForm.searchBeginDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择开始时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" style="margin-left: 10px;">
            <el-date-picker
                style="display: inline-block;"
                v-model="searchForm.searchEndDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择结束时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-button-group style="margin-left: 16px;">
            <el-button type="primary" plain icon="el-icon-search" @click="search">搜索</el-button>
            <el-button @click="refresh">重置</el-button>
          </el-button-group>
        </div>
      </el-form>
    </div>

    <el-divider />

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" stripe>
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="车牌号" prop="carNo" />
      <el-table-column label="登记时间" prop="createTime" />
      <el-table-column label="登记人" prop="name" />
      <el-table-column label="司机姓名" prop="driverName" />
      <el-table-column label="司机电话" prop="driverPhone" />
      <el-table-column label="有效期始" prop="visitBeginDate" />
      <el-table-column label="有效期止" prop="visitEndDate" />
      <el-table-column label="审批状态">
        <template slot-scope="scope">
          {{ scope.row.status=='0'? '待审' : (scope.row.status=='1' ? '通过':'未通过') }}
        </template>
      </el-table-column>
      <el-table-column label="入场状态">
        <template slot-scope="scope">
          {{ scope.row.carStatus=='0'? '未入场' : (scope.row.carStatus=='1' ? '已入场':'已出场') }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="80" align="center">
        <template slot-scope="scope">
          <span>
            <el-button circle type="success" icon="el-icon-more" size="mini" @click="detail(scope.row.id)" title="详情"></el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
      @pagination="fetchData" style="text-align: center" />

    <el-dialog :visible.sync="dialogVisible" title="详情" width="65%">
      <el-form ref="dataForm" :model="detailData" label-width="120px" label-position="right" v-if="detailData">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="ID号">
                  <span>{{ detailData.id }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="审批状态">
                  <span>{{ detailData.status==0? '未审' : (detailData.status==1 ? '通过':'未通过') }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="登记时间">
                  <span>{{ detailData.optionTime }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接待人">
                <span>{{ detailData.receiverUserName }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接待人手机">
                <span>{{ detailData.phone }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接待部门">
                  <span>{{ detailData.receiverDeptName }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="有效期始">
                  <span>{{ detailData.visitBeginDate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="有效期止">
                  <span>{{ detailData.visitEndDate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="重复进入">
                  <span>{{ detailData.reaptIo === 1 ? '允许' : '禁止' }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="车牌号">
                  <span>{{ detailData.cars[0].carNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="司机姓名">
                  <span>{{ detailData.cars[0].driverName }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号">
                  <span>{{ detailData.cars[0].driverIdcard }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="驾驶员电话">
                  <span>{{ detailData.cars[0].driverPhone }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="进车原因">
                  <span>{{ detailData.visitObjective }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注">
                  <span>{{ detailData.remarks }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="同行人">
                  <template v-for="(item1,index1) in detailData.cars[0].persons">
                    <el-tooltip class="item" effect="dark" :content="'手机号：' + item1.phone + '，身份证号：' + item1.idcard" placement="top">
                      <el-tag>{{item1.name}}</el-tag>
                    </el-tooltip>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="8">
            <template v-if="detailData.views.length > 0">
              <el-col :span="24">
                <el-card class="box-card" shadow="never">
                  <div slot="header" class="clearfix">
                    <span>审批信息</span>
                  </div>
                  <el-timeline>
                    <el-timeline-item
                        v-for="(item,index) in detailData.views"
                        :key="index"
                        :timestamp="formatTime(item.date)">
                      <span>审批人：{{item.name}}</span>
                      <span><br/>审批结果：
                        <template v-if="item.examineStatus === 1">
                          <el-tag type="success">通过</el-tag>
                        </template>
                        <template v-else>
                          <el-tag type="danger">不通过</el-tag>
                        </template>
                      </span>
                      <template v-if="item.view">
                        <br/>
                        <span>审批意见：{{item.view}}</span>
                      </template>
                    </el-timeline-item>
                  </el-timeline>
                </el-card>
                <template v-if="detailData.inOuts.length > 0">
                <el-card class="box-card" shadow="never">
                  <div slot="header" class="clearfix">
                    <span>出入信息</span>
                  </div>
                  <el-timeline>
                    <el-timeline-item
                        v-for="(item,index) in detailData.inOuts"
                        :key="index"
                        :timestamp="formatTime(item.date)">
                      <span>{{item.date}}</span>
                      <span>由{{item.gateName}}</span>
                      <span>
                        <template v-if="item.direction === 1">
                          <el-tag>出场</el-tag>
                        </template>
                        <template v-else>
                          <el-tag type="warning">入场</el-tag>
                        </template>
                      </span>
                    </el-timeline-item>
                  </el-timeline>
                </el-card>
                </template>
              </el-col>
            </template>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {
    carList,
    carInfo,
    carExport
  } from '@/api/short'
  import {datetimeFormat} from '@/utils/datetime-util'
  import {
    deptList
  } from '@/api/process'
  import { exportData } from '@/api/long'

  export default {
    name: 'temporaryVehicle',
    components: {
      Pagination
    },
    data() {
      return {
        statusOptions: [
          {value: '', label: '全部'},
          {value: '0', label: '待审'},
          {value: '1', label: '已通过'},
          {value: '2', label: '已拒绝'},
        ],
        deptOptions: [],
        total: 0,
        list: [],
        listLoading: false,
        searchForm: {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
          status: '',
          // deptId: ''
        },
        dialogVisible: false,
        dialogType: 'create',
        loading: false,
        detailData: null,
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      }
    },
    created() {
      this.getDeptList()
    },
    mounted() {
      this.fetchData()
    },
    methods: {
      exportData() {
        let queryParams = {
          searchValue: this.searchForm.searchValue,
          searchBeginDate: this.searchForm.searchBeginDate,
          searchEndDate: this.searchForm.searchEndDate,
          status: this.searchForm.status,
        }
        carExport(queryParams).then(response => {
          if(response.code === 200) {
            let filename = response.msg
            let url = '/download/' + filename
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", filename); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
          }
        })
      },
      getDeptName(deptId) {
        if (deptId === '1') {
          return ''
        }
        let name = '未知'
        for (let i = 0; i < this.deptOptions.length; i++) {
          if (this.deptOptions[i].value === deptId) {
            let parentName = ''
            if (this.deptOptions[i].parentId !== 1) {
              // 找上级部门
              for (let j = 0; j < this.deptOptions.length; j++ ) {
                if (this.deptOptions[j].value === this.deptOptions[i].parentId) {
                  parentName = this.deptOptions[j].label
                }
              }
            }
            if (parentName) {
              name = parentName + '-'  + this.deptOptions[i].label
            } else {
              name = this.deptOptions[i].label
            }
            break
          }
        }
        return name
      },
      getDeptList() {
        deptList().then(response => {
          console.log('getDeptList', response)
          if (response.code === 200) {
            this.deptOptions = []
            for (let i = 0; i < response.rows.length; i++) {
              let obj = response.rows[i]
              this.deptOptions.push({
                value: '' + obj.deptId,
                label: obj.name,
                parentId: '' + obj.parentId
              })
            }
            console.log('this.deptOptions', this.deptOptions)
          } else {
            this.$message.error('获取部门列表失败' + response.errmsg)
          }
        })
      },
      formatTime(timeStr) {
        return datetimeFormat('YYYY-mm-dd HH:MM:SS', new Date(timeStr))
      },
      search() {
        this.fetchData()
      },
      refresh() {
        this.searchForm = {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
          status: '',
          // deptId: ''
        }
        this.fetchData()
      },
      fetchData() {
        this.listLoading = true
        carList(this.searchForm).then(response => {
          this.list = response.rows
          this.total = response.total
          this.listLoading = false
        })
      },
      detail(id) {
        carInfo(id).then(response => {
          if (response.code === 200) {
            this.detailData = response.data
            this.dialogVisible = true
          } else {
            this.$message.error('获取详情失败：' + response.msg)
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-upload {
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    .el-icon-plus.avatar-uploader-icon {
      border: 1px dashed #d9d9d9 !important;
      border-radius: 6px;
      font-size: 28px;
      color: #8c939d;
      width: 128px;
      height: 128px;
      line-height: 128px;
      text-align: center;
    }
  }

  .avatar-uploader {
    height: 128px;

    img {
      width: 128px;
      height: 128px;
    }
  }
</style>
